package com.example.admin.goodnews.fragment.ShengHuoChangYong;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.admin.goodnews.R;
import com.example.admin.goodnews.adapter.LiShiAdapter;
import com.example.admin.goodnews.entity.LiShiDataEntity;
import com.example.admin.goodnews.utils.GetCurrentTimeUtil;
import com.example.admin.goodnews.utils.HttpUtils;
import com.example.admin.goodnews.utils.NetWorkDataUrl;
import com.google.gson.Gson;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 历史上的今天页面
 * Created by admin on 2016/10/15.
 */

public class LishiFragment extends Fragment {
    private RecyclerView recyclerview_lishi;
    private TextView tv_time;// 显示今天的日期
    private LiShiDataEntity liShiDataEntity;// 数据实体类
    private LiShiAdapter liShiAdapter;// 数据适配器
    private LinearLayoutManager linearLayoutManager;// 布局管理器
    String date = GetCurrentTimeUtil.getDate();// 获取今天日期
    String alldate = GetCurrentTimeUtil.getAllDate();
    String time = GetCurrentTimeUtil.getCurrentTime();// 获取系统当前时间
    String datetime = GetCurrentTimeUtil.getDateTime();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_lishi, null);

        tv_time = (TextView) view.findViewById(R.id.tv_time);
        recyclerview_lishi = (RecyclerView) view.findViewById(R.id.recyclerview_lishi);
        linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerview_lishi.setLayoutManager(linearLayoutManager);// 设置布局管理器
        Date date1 = new Date(alldate);
        tv_time.setText("今天是：" + datetime + "  " + GetCurrentTimeUtil.getWeekOfDate(date1));

        getData();
        return view;
    }

    private void getData() {
        String param = NetWorkDataUrl.LISHISHANGDEJINTIAN_PARAM1 + date + NetWorkDataUrl.LISHISHANGDEJINTIAN_PARAM2 + time + NetWorkDataUrl.LISHISHANGDEJINTIAN_PARAM3;
        try {
            HttpUtils.doPostAsyn(NetWorkDataUrl.LISHISHANGDEJINTIAN_URL, param, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    liShiDataEntity = gson.fromJson(result, LiShiDataEntity.class);
                    if (liShiDataEntity.getShowapi_res_body().getList().size() > 0) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    break;
                case 1:
                    liShiAdapter = new LiShiAdapter(getActivity(), liShiDataEntity);
                    recyclerview_lishi.setAdapter(liShiAdapter);
                    break;
            }
        }
    };

}
